package com.doubi.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doubi.constants.SystemConstants;
import com.doubi.mapper.SysUserRoleMapper;
import com.doubi.model.sys.TSysUser;
import com.doubi.model.sys.TSysUserRole;
import com.doubi.utils.DataUtil;
import com.doubi.utils.MD5Util;
import com.doubi.vo.AppModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;

@Service("sysUserRoleService")
public class SysUserRoleService extends ServiceImpl<SysUserRoleMapper,TSysUserRole> {

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;
    @Autowired
    private SysUserService sysUserService;

    /**
     * 功能说明：保存用户+部门
     * @author  MrDoubi 2019-06-28 15:37:13
     * @param sysUser 用户bean
     * @return model
     */
    public AppModel saveSysUser(TSysUser sysUser, String roleOid){
        //修改
        if(DataUtil.isNotEmpty(sysUser.getOid())){
            TSysUser bean = sysUserService.getById(sysUser.getOid());
            bean.setAccount(sysUser.getAccount());
            bean.setName(sysUser.getName());
            bean.setSex(sysUser.getSex());
            bean.setPhone(sysUser.getPhone());
            bean.setEmail(sysUser.getEmail());
            bean.setUpdateTime(new Date());
            bean.setDeptOid(sysUser.getDeptOid());
            sysUserService.updateById(bean);
            QueryWrapper<TSysUserRole> wrapper = new QueryWrapper<>();
            wrapper.eq("user_oid",sysUser.getOid());
            TSysUserRole sysUserRole = sysUserRoleMapper.selectOne(wrapper);
            if(DataUtil.isNotEmpty(sysUserRole)){
                sysUserRole.setRoleOid(roleOid);
                sysUserRoleMapper.updateById(sysUserRole);
            }else {
                sysUserRole = new TSysUserRole();
                sysUserRole.setRoleOid(roleOid);
                sysUserRole.setUserOid(sysUser.getOid());
                sysUserRoleMapper.insert(sysUserRole);
            }
        }else {
            sysUser.setPassword(MD5Util.md5Hex(SystemConstants.DEFAULT_PASS_WORD));
            sysUser.setCreateTime(new Date());
            sysUser.setUpdateTime(new Date());
            sysUser.setIsDelete(1);
            sysUser.setUserType(SystemConstants.UserType.ADMIN);
            sysUser.setLockStatus("N");
            sysUser.setIsAble("Y");
            sysUser.setFailNum(0);
            sysUserService.save(sysUser);
            TSysUserRole sysUserRole = new TSysUserRole();
            sysUserRole.setRoleOid(roleOid);
            sysUserRole.setUserOid(sysUser.getOid());
            sysUserRoleMapper.insert(sysUserRole);
        }
        return new AppModel(0,"操作成功！");
    }

    /**
     * 功能描述：删除用户角色关联
     * @author   MrDoubi 2019-07-02 16:07
     * @param roleOid           角色oid
     */
    public void deleteAllByRoleOid(String roleOid){
        sysUserRoleMapper.deleteByMap(new HashMap<String, Object>(){{
            put("role_oid",roleOid);
        }});
    }

}
